package com.hn.project.rqinspect.controller;

import com.hn.common.constant.RedisConstant;
import com.hn.common.utils.SecurityUtils;
import com.hn.doc.xyj.annotation.Api;
import com.hn.framework.web.controller.BaseController;
import com.hn.framework.web.domain.AjaxResult;
import com.hn.framework.web.page.TableDataInfo;
import com.hn.project.rqinspect.domain.database.TaskCheckChangeRecord;
import com.hn.project.rqinspect.domain.database.TaskCheckRecord;
import com.hn.project.rqinspect.domain.dto.QuesCheckRecordDto;
import com.hn.project.rqinspect.domain.dto.QuesCheckRecordNumDto;
import com.hn.project.rqinspect.domain.dto.TaskCheckRecordDto;
import com.hn.project.rqinspect.domain.dto.TaskQuesCheckRecordDetailDto;
import com.hn.project.rqinspect.domain.vo.TaskCheckRecordVo;
import com.hn.project.rqinspect.service.TaskCacheService;
import com.hn.project.rqinspect.service.TaskCheckService;
import com.hn.utils.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务巡检
 *
 * @author admin
 */
@Api("任务巡检")
@RestController
@RequestMapping("taskCheck")
public class TaskCheckController extends BaseController {

    @Autowired
    private TaskCheckService taskCheckService;

    @Autowired
    private TaskCacheService taskCacheService;

    @Value("${upload.web.url}")
    private String uploadWebUrl;

    /**
     * 查询任务记录下的检查记录
     */
    @Api(value = "查询任务记录下的检查记录", result = TaskCheckRecordDto.class)
    @GetMapping("list")
    public TableDataInfo listCheckRecord(TaskCheckRecordVo taskCheckRecordVo) {
        Integer taskRecordId = taskCheckRecordVo.getTaskRecordId();
        AssertUtils.notNull(taskRecordId, "请选择对应任务");
        startPage();
        List<TaskCheckRecordDto> taskCheckRecordDtos = taskCheckService.listCheckRecordDto(taskCheckRecordVo);
        return getDataTable(taskCheckRecordDtos);
    }

    /**
     * 添加评价
     * 注意：如果是好评,一个部位(不是部位类型,是网点下的部位名称)只能加一个
     * 比如 仓库区有仓库1、仓库2、仓库3,那仓库1只有一个好评,仓库2只有一个好评,仓库3只有一个好评
     */
    @Api(value = "添加检查记录")
    @PostMapping("add")
    public AjaxResult addCheckRecord(@RequestBody TaskCheckRecord taskCheckRecord) {
        Integer userId = SecurityUtils.getUserId();
        taskCheckRecord.setUserId(userId);
        return toAjax(taskCheckService.addCheckRecord(taskCheckRecord));
    }

    /**
     * 检查记录详情
     */
    @Api(value = "检查记录详情", result = TaskCheckRecordDto.class)
    @GetMapping("get")
    public AjaxResult getCheckRecord(Integer checkRecordId) {
        TaskCheckRecordDto checkRecordDto = taskCheckService.getCheckRecordDto(checkRecordId);
        return AjaxResult.success(checkRecordDto);
    }

    /**
     * 修改检查记录
     */
    @Api(value = "修改检查记录")
    @PostMapping("update")
    public AjaxResult updateCheckRecord(@RequestBody TaskCheckRecord taskCheckRecord) {
        return toAjax(taskCheckService.updateCheckRecord(taskCheckRecord));
    }

    /**
     * 删除检查记录
     */
    @Api(value = "删除检查记录")
    @GetMapping("del")
    public AjaxResult del(Integer checkRecordId) {
        return toAjax(taskCheckService.delCheckRecord(checkRecordId));
    }

    /**
     * 根据任务记录id删除检查记录
     */
    @Api(value = "根据任务记录id删除检查记录")
    @GetMapping("delByTaskRecordId")
    public AjaxResult delByTaskRecordId(Integer taskRecordId) {
        return toAjax(taskCheckService.delByTaskRecordId(taskRecordId));
    }

    /**
     * 新增整改记录
     */
    @Api(value = "新增整改记录")
    @PostMapping("addChangeRecord")
    public AjaxResult addChangeRecord(@RequestBody TaskCheckChangeRecord checkChangeRecord) {
        checkChangeRecord.setUserId(SecurityUtils.getUserId());
        return toAjax(taskCheckService.addChangeRecord(checkChangeRecord));
    }

    /**
     * 【代办详情】->【该网点历史巡检记录】->【未处理问题】
     * 网点未处理问题列表
     */
    @Api(value = "网点未处理问题列表", result = QuesCheckRecordDto.class)
    @GetMapping("listUnProcessCheckRecord")
    public TableDataInfo listUnProcessCheckRecord(Integer branchId) {
        AssertUtils.notNull(branchId, "请先选择网点");
        startPage();
        return getDataTable(taskCheckService.listQuesCheckRecord(branchId));
    }

    /**
     * 问题统计
     *
     * @param type 1=巡检干部 2=网点的
     */
    @Api(value = "问题统计", result = QuesCheckRecordNumDto.class)
    @GetMapping("countQuesCheckRecordNum")
    public AjaxResult countQuesCheckRecordNum(Integer type,Integer taskType) {
        AssertUtils.notNull(type,"type 必传");
        return AjaxResult.success(taskCheckService.countQuesCheckRecord(type,taskType));
    }

    /**
     *   检查问题问题列表
     *  @param type 1=巡检干部 2=网点的
     * @param followStatus 1=已整改 2=待整改
     */
    @Api(value = "问题列表", result = QuesCheckRecordDto.class)
    @GetMapping("listQuesCheckRecord")
    public AjaxResult listQuesCheckRecord(Integer type,Integer taskType,Integer followStatus) {
        AssertUtils.notNull(type,"type 必传");
        AssertUtils.notNull(followStatus,"followStatus 必传");
        return AjaxResult.success(taskCheckService.listQuesCheckRecord(type,taskType,followStatus));
    }

    /**
     *  检查问题详情
     */
    @Api(value = "问题详情", result = TaskQuesCheckRecordDetailDto.class)
    @GetMapping("getQuesCheckRecord")
    public AjaxResult getQuesCheckRecord(Integer id) {
        AssertUtils.notNull(id,"id 必传");
        return AjaxResult.success(taskCheckService.checkRecordDetail(id));
    }

    /**
     * 问题详情已阅
     */
    @Api(value = "问题详情已阅")
    @GetMapping("updateCheckRecordReadFlag")
    public AjaxResult updateCheckRecordReadFlag(Integer id) {
        AssertUtils.notNull(id,"id 必传");
        Integer userId = SecurityUtils.getUserId();
        taskCacheService.reduce(RedisConstant.CHECK_CHANGE_QUES,userId,id);
        return AjaxResult.success();
    }

}
